﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;

namespace messageboard.dal
{
    public class MessageDal
    {
        private SQLHelper sqlhelper = null;
        public MessageDal()
        {
            sqlhelper =new SQLHelper();

        }

        #region 查看留言
        public DataTable showmessage()
        {
            string cmdText = "select _user.name,_user.face,_message.message,_message.datetime,_message.title,_message.messageid from _user join _message on _user.id=_message.id order by _message.datetime desc";
            DataTable dt = sqlhelper.ExecuteQuery(cmdText, CommandType.Text);
            return dt;

        } 
        #endregion
        #region 添加留言
        public bool addmessage(string message, string title,int id)
        {
            bool flag = false;
            string cmdText = "insert into _message (message,datetime,id,title) values(@message,getdate(),@id,@title)";
            SqlParameter[] paras = new SqlParameter[] { 
                new SqlParameter("@message",message),
            new SqlParameter("@id",id),
            new SqlParameter("@title",title)
            };
            int re = sqlhelper.ExecuteNonQuery(cmdText, paras, CommandType.Text);
            if (re > 0)
            {
                flag = true;
            }
            return flag;
        } 
        #endregion
        #region 按内容搜索留言
        public DataTable searchmessagebycontent(string message)
        {

            string cmdText = "search";
            SqlParameter[] paras = new SqlParameter[] { 
                new SqlParameter("@message",message)
            };
            DataTable dt = sqlhelper.ExecuteQuery(cmdText, paras, CommandType.StoredProcedure);
            return dt;
            
            
        }
        #endregion
        #region 删除留言
        public bool deletemessage(int messageid)
        {
            bool flag = false;
            string cmdText = "delete _message where messageid=@messageid ";
            SqlParameter[] paras = new SqlParameter[]
           {    
               new SqlParameter("@messageid",messageid),
             
             };
            int a = sqlhelper.ExecuteNonQuery(cmdText, paras, CommandType.Text);
            if (a > 0)
            {
                flag = true;
            }
            return flag;
        }
        #endregion
        
    }
}